import GameManager from "./GameManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

	@property
	init_speed = 150;

	@property
	a_power = 600;

	@property
	y_radio = 0.556;

	@property(cc.Node)
	game: cc.Node = null
	game_manager = null;

	@property(cc.AudioClip)
	audioJumpPressOnce: cc.AudioClip = null

	@property(cc.AudioClip)
	audioJumpPressRepeat: cc.AudioClip = null

	@property(cc.AudioClip)
	audioJumpSuccess: cc.AudioClip = null

	@property(cc.AudioClip)
	audioJumpFail: cc.AudioClip = null

	@property(cc.ParticleSystem)
	pressParticle: cc.ParticleSystem = null

	// LIFE-CYCLE CALLBACKS:
	next_block = null
	direction: number = 1;
	jumping: boolean = false;
	motionStreak: cc.MotionStreak = null

	rot_node: cc.Node = null
	anim_node: cc.Node = null

	is_power_mode: boolean = false
	speed: number = 0;
	x_distance: number = 0;

	onLoad() {
		this.motionStreak = this.getComponent(cc.MotionStreak);
		this.game_manager = this.game.getComponent(GameManager);
		this.rot_node = this.node.getChildByName("rotate");
		this.anim_node = this.rot_node.getChildByName("piece");
	}

	start() {
		this.node.parent.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
		this.node.parent.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
		this.node.parent.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
	}

	// update (dt) {}

	onTouchStart(e: cc.Event.EventTouch) {
		if (this.game_manager.mapMoving || this.jumping) {
			return;
		}

		this.is_power_mode = true;			//是否准备蓄力跳跃
		this.x_distance = 0;
		this.speed = this.init_speed;
		this.pressParticle.enabled = true;
		this.pressParticle.resetSystem();
		this.playTouchSound();

		this.anim_node.stopAllActions();
		cc.tween(this.anim_node)
			.to(2, { scaleY: 0.5 })
			.start();
	}

	onTouchEnd(e: cc.Event.EventTouch) {
		this.player_redey();
	}

	player_redey() {
		if (!this.is_power_mode) {
			return;
		}
		this.is_power_mode = false;
		this.anim_node.stopAllActions();
		this.anim_node.runAction(cc.scaleTo(0.5, 1, 1));
		cc.tween(this.anim_node)
			.to(0.5, { scaleY: 1 })
			.start();
		this.pressParticle.enabled = false;
		this.stopTouchSound();
		this.player_jump();		//玩家跳跃
	}

	player_jump() {
		this.motionStreak.enabled = true;
		this.jumping = true;

		let x_distance = this.x_distance * this.direction;
		let y_distance = this.x_distance * this.y_radio;

		let target_pos = this.node.position;
		target_pos.x += x_distance;
		target_pos.y += y_distance;

		// 
		//this.rot_node.runAction(cc.rotateBy(0.5, 360 * this.direction));
		cc.tween(this.rot_node)
			.by(0.5, { angle: -360 * this.direction})
			.start();
		let w_pos = this.node.parent.convertToWorldSpaceAR(target_pos);
		let is_game_over = false;

		//计算分数
		//let jumpScore = this.next_block.is_jump_on_block(w_pos, this.direction);
		let jumpScore=this.next_block.isJumpOnBlock(w_pos,this.direction);
		if (jumpScore > 0) {
			target_pos = this.node.parent.convertToNodeSpaceAR(w_pos); // target_pos就变成了参考点的位置;
			y_distance = target_pos.sub(this.node.position).y;
		} else {
			cc.log('游戏结束')
			is_game_over = true;
		}

		let j = cc.jumpTo(0.5, cc.v2(target_pos), 200, 1);
		this.direction = (Math.random() < 0.5) ? -1 : 1;

		let end_func = cc.callFunc(function () {
			this.motionStreak.enabled = false;
			this.jumping = false;

			if (is_game_over) {
				this.game_manager.on_checkout_game();
				cc.audioEngine.playEffect(this.audioJumpFail, false);
			} else {
				if (this.direction === -1) {
					this.game_manager.move_map(580 - w_pos.x, -y_distance);
				} else {
					this.game_manager.move_map(180 - w_pos.x, -y_distance);
				}

				let playerPosWorld = this.node.parent.convertToWorldSpaceAR(this.node.getPosition());
				this.game_manager.gainScore(playerPosWorld, jumpScore);

				cc.audioEngine.playEffect(this.audioJumpSuccess, false);
			}
		}.bind(this));

		let seq = cc.sequence(j, end_func);
		this.node.runAction(seq);
	}

	set_next_block(block: cc.Node) {
		this.next_block = block;
	}



	audioIDPressOnce: number;				//播放音乐id
	audioPressRepeatAlreadyPlay: boolean;
	audioPressRepeatCallback: Function;
	audioIDPressRepeat: number;

	//播放音乐
	playTouchSound() {
		this.audioIDPressOnce = cc.audioEngine.playEffect(this.audioJumpPressOnce, false);
		let audioTimePressOnce = cc.audioEngine.getDuration(this.audioIDPressOnce);

		this.audioPressRepeatAlreadyPlay = false;
		this.audioPressRepeatCallback = function () {
			this.audioPressRepeatAlreadyPlay = true;
			this.audioIDPressRepeat = cc.audioEngine.playEffect(this.audioJumpPressRepeat, true)
		};

		//大音效播放完之后，开始重复播放小音效
		this.scheduleOnce(this.audioPressRepeatCallback, audioTimePressOnce);
	}

	//停止音乐播放
	stopTouchSound() {
		if (this.audioPressRepeatAlreadyPlay) {
			cc.audioEngine.stopEffect(this.audioIDPressRepeat);
		} else {
			cc.audioEngine.stopEffect(this.audioIDPressOnce);
			this.unschedule(this.audioPressRepeatCallback);
		}
	}

	update(dt: number) {
		if (this.is_power_mode) {
			this.speed += (this.a_power * dt);
			this.x_distance += this.speed * dt;
		}
	}
}
